package com.sz.mes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diboot.core.binding.Binder;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.vo.JsonResult;
import com.diboot.iam.entity.IamUser;
import com.diboot.iam.mapper.IamUserMapper;
import com.diboot.iam.util.IamSecurityUtils;
import com.sz.mes.dto.OutboundDTO;
import com.sz.mes.entity.*;
import com.sz.mes.entity.warehouse.Lattice;
import com.sz.mes.entity.warehouse.LatticeGoods;
import com.sz.mes.entity.warehouse.OutboundRecord;
import com.sz.mes.mapper.*;
import com.sz.mes.mapper.warehouse.LatticeGoodsMapper;
import com.sz.mes.mapper.warehouse.LatticeMapper;
import com.sz.mes.mapper.warehouse.OutboundRecordMapper;
import com.sz.mes.service.OutboundService;
import com.sz.mes.service.warehouse.LatticeService;
import com.sz.mes.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 出库相关Service实现
 *
 * @author hem
 * @version 1.0
 * @date 2023-05-25
 * Copyright © szhs
 */
@Service
@Slf4j
public class OutboundServiceImpl extends BaseCustomServiceImpl<OutboundMapper, Outbound> implements OutboundService {

    private final OutboundMapper outboundMapper;
    private final OutboundRecordMapper outboundRecordMapper;
    private final LatticeGoodsMapper latticeGoodsMapper;
    private final OutboundLatticeMapper outboundLatticeMapper;
    private final LatticeService latticeService;
    private final PicklistMapper picklistMapper;
    private final PlanDemandQuantityMapper planDemandQuantityMapper;
    private final LatticeMapper latticeMapper;
    private final ProductTaskMapper productTaskMapper;
    private final ReceiveMapper receiveMapper;
    private final IamUserMapper iamUserMapper;
    private final SaleOrderMapper saleOrderMapper;
    private final ShipMapper shipMapper;
    private final IssueDocMapper issueDocMapper;
    private final GoodsMapper goodsMapper;
    private final MiscShipmentMapper miscShipmentMapper;

    public OutboundServiceImpl(OutboundMapper outboundMapper, OutboundRecordMapper outboundRecordMapper,
                               LatticeGoodsMapper latticeGoodsMapper, OutboundLatticeMapper outboundLatticeMapper,
                               LatticeService latticeService, PicklistMapper picklistMapper,
                               PlanDemandQuantityMapper planDemandQuantityMapper, LatticeMapper latticeMapper,
                               ProductTaskMapper productTaskMapper, ReceiveMapper receiveMapper,
                               IamUserMapper iamUserMapper, SaleOrderMapper saleOrderMapper, ShipMapper shipMapper, IssueDocMapper issueDocMapper, GoodsMapper goodsMapper, MiscShipmentMapper miscShipmentMapper) {
        this.outboundMapper = outboundMapper;
        this.outboundRecordMapper = outboundRecordMapper;
        this.latticeGoodsMapper = latticeGoodsMapper;
        this.outboundLatticeMapper = outboundLatticeMapper;
        this.latticeService = latticeService;
        this.picklistMapper = picklistMapper;
        this.planDemandQuantityMapper = planDemandQuantityMapper;
        this.latticeMapper = latticeMapper;
        this.productTaskMapper = productTaskMapper;
        this.receiveMapper = receiveMapper;
        this.iamUserMapper = iamUserMapper;
        this.saleOrderMapper = saleOrderMapper;
        this.shipMapper = shipMapper;
        this.issueDocMapper = issueDocMapper;
        this.goodsMapper = goodsMapper;
        this.miscShipmentMapper = miscShipmentMapper;
    }

    @Override
    public JsonResult otherOperation(OutboundDTO entity) {
        //扫码时检查库位是否已被删除或是扫描出来的是其它环境的库位id
        if (!CollectionUtils.isEmpty(entity.getOutFromLatticeList())) {
            for (ShelfIdAndGoodQuantityVO shelfIdAndGoodQuantityVO : entity.getOutFromLatticeList()) {
                Lattice lattice = latticeService.findById(shelfIdAndGoodQuantityVO.getLatticeId());
                if (lattice == null) {
                    return JsonResult.FAIL_NOT_FOUND("二维码对应的库位存在异常");
                }
            }
        }

        Outbound outbound = new Outbound();
        Integer quantity = 0;
        for (ShelfIdAndGoodQuantityVO shelfIdAndGoodQuantityVO : entity.getOutFromLatticeList()) {
            outbound.setGoodsId(shelfIdAndGoodQuantityVO.getGoodsId());
            outbound.setGoodsCode(shelfIdAndGoodQuantityVO.getGoodsCode());
            outbound.setGoodsName(shelfIdAndGoodQuantityVO.getGoodsName());
            outbound.setSpecifications(shelfIdAndGoodQuantityVO.getSpecifications());
            outbound.setUnit(shelfIdAndGoodQuantityVO.getUnit());


            LocalDate currentDate = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            Lattice lattice = latticeService.findById(shelfIdAndGoodQuantityVO.getLatticeId());

            quantity += shelfIdAndGoodQuantityVO.getQuantity();

            //货格关联商品表（库存数量表更新）
            QueryWrapper<LatticeGoods> wrapper = new QueryWrapper<>();
            wrapper.eq("goods_id", shelfIdAndGoodQuantityVO.getGoodsId());
            wrapper.eq("warehouse_lattice_id", shelfIdAndGoodQuantityVO.getLatticeId());
            LatticeGoods latticeGoods = latticeGoodsMapper.selectOne(wrapper);
            if (latticeGoods != null) {
                if (latticeGoods.getQuantity() - shelfIdAndGoodQuantityVO.getQuantity() == 0) {
                    latticeGoodsMapper.deleteByIdRealy(latticeGoods.getId());
                } else if (latticeGoods.getQuantity() - shelfIdAndGoodQuantityVO.getQuantity() < 0) {
                    return JsonResult.FAIL_OPERATION("超出库存范围");
                } else {
                    latticeGoods.setQuantity(latticeGoods.getQuantity() - shelfIdAndGoodQuantityVO.getQuantity());
                    latticeGoodsMapper.updateById(latticeGoods);
                }
            } else {
                return JsonResult.FAIL_EXCEPTION("不存在该库存");
            }

            //出库关联货格表
            OutboundLattice outboundLattice = new OutboundLattice();
            outboundLattice.setOutboundId(entity.getId());
            outboundLattice.setGoodsId(shelfIdAndGoodQuantityVO.getGoodsId());
            outboundLattice.setWarehouseLatticeId(shelfIdAndGoodQuantityVO.getLatticeId());
            outboundLattice.setQuantity(shelfIdAndGoodQuantityVO.getQuantity());
            outboundLattice.setLabel(lattice.getLabel());
            outboundLatticeMapper.insert(outboundLattice);

            //出库记录表
            OutboundRecord outboundRecord = new OutboundRecord();
            outboundRecord.setOutboundId(entity.getId());
            outboundRecord.setGoodsId(shelfIdAndGoodQuantityVO.getGoodsId());
            outboundRecord.setGoodsCode(shelfIdAndGoodQuantityVO.getGoodsCode());
            outboundRecord.setQuantity(shelfIdAndGoodQuantityVO.getQuantity());
            outboundRecord.setWarehouseLatticeId(shelfIdAndGoodQuantityVO.getLatticeId());
            outboundRecord.setType(entity.getOutboundType());
            outboundRecord.setTaskNum(shelfIdAndGoodQuantityVO.getOrderNum());
            outboundRecordMapper.insert(outboundRecord);


            if (entity.getOutboundType().equals("SCCK")) {

                IssueDoc issueDoc = new IssueDoc();
                QueryWrapper<ProductTask> productTaskQueryWrapper = new QueryWrapper<>();
                productTaskQueryWrapper.eq("task_num", entity.getTaskNum());
                List<ProductTask> productTaskList = productTaskMapper.selectList(productTaskQueryWrapper);
                ProductTask productTask = productTaskList.get(0);
                issueDoc.setReceiptNum("LL" + currentDate.format(formatter).replaceAll("-", ""));


                QueryWrapper<IssueDoc> issueDocQueryWrapper = new QueryWrapper<>();
                issueDocQueryWrapper.eq("product_order_num", entity.getReceiptNum());
                List<IssueDoc> issueDocList = issueDocMapper.selectList(issueDocQueryWrapper);
                if (issueDocList.size() == 0) {
                    issueDoc.setRowNum("10");
                } else {
                    issueDoc.setRowNum(String.valueOf(issueDocList.size() * 10));
                }
                LocalDate localDate = LocalDate.parse(currentDate.format(formatter), formatter);
                Date date = java.sql.Date.valueOf(localDate);
                issueDoc.setBusinessDate(productTask.getStartTime());
                issueDoc.setConfirmationDate(date);
                issueDoc.setProductOrderNum(productTask.getReceiptNum());
                issueDoc.setProductNum(productTask.getProductNum());
                issueDoc.setProductName(productTask.getProductName());
                issueDoc.setProductSpecification(productTask.getProductSpecification());
                issueDoc.setQuantity(productTask.getQuantity());
                issueDoc.setStockNum(shelfIdAndGoodQuantityVO.getGoodsCode());
                issueDoc.setStockName(shelfIdAndGoodQuantityVO.getGoodsName());
                issueDoc.setStockSpecification(shelfIdAndGoodQuantityVO.getSpecifications());
                issueDoc.setRequireQuantity(productTask.getStockDosage());
                issueDoc.setActualQuantity(String.valueOf(shelfIdAndGoodQuantityVO.getQuantity()));
//                List<ShelfIdAndGoodQuantityVO> listDatas = entity.getOutFromLatticeList();
//                int quantitys = 0;
//                for (ShelfIdAndGoodQuantityVO shelfIdAndGoodQuantityVO1 : listDatas) {
//                    quantitys = quantitys + shelfIdAndGoodQuantityVO1.getQuantity();
//                }
//                issueDoc.setUnit(String.valueOf(quantitys));
//                issueDoc.setLocation();
                issueDoc.setUnit(shelfIdAndGoodQuantityVO.getUnit());
                issueDoc.setProductOrderDepartment(productTask.getProductSection());
                issueDoc.setReturnNum(entity.getReturnNum());
                issueDocMapper.insert(issueDoc);
            } else if (entity.getOutboundType().equals("FSCCK")) {
                //erp对接表
                Ship ship = new Ship();
                QueryWrapper<SaleOrder> saleOrderQueryWrapper = new QueryWrapper<>();

                saleOrderQueryWrapper.eq("order_num", shelfIdAndGoodQuantityVO.getOrderNum());
                saleOrderQueryWrapper.eq("order_row", shelfIdAndGoodQuantityVO.getOrderRow().trim());
                SaleOrder saleOrder = saleOrderMapper.selectOne(saleOrderQueryWrapper);

                QueryWrapper<Ship> shipQueryWrapper = new QueryWrapper<>();
                shipQueryWrapper.eq("sale_order", shelfIdAndGoodQuantityVO.getOrderNum());
                List<Ship> shipList = shipMapper.selectList(shipQueryWrapper);

                if (shipList.size() == 0) {
                    ship.setRowNum("10");
                } else {
                    ship.setRowNum(String.valueOf(shipList.size() * 10));
                }
                LocalDate localDate = LocalDate.parse(currentDate.format(formatter), formatter);
                Date date = java.sql.Date.valueOf(localDate);
                ship.setReceiptDate(date);

                ship.setReceiptNum("YMS" + currentDate.format(formatter).replaceAll("-", ""));
                ship.setCustomerName(saleOrder.getCustomerName());
                ship.setSalesman(saleOrder.getBusinessManager());
                ship.setCustomerOrder(saleOrder.getOrderNum());
                ship.setCustomerRow(saleOrder.getOrderRow());
                ship.setProductNum(saleOrder.getProductNum());
                ship.setProductName(saleOrder.getProductName());
                ship.setProductSpecification(saleOrder.getSpecification());

                List<ShelfIdAndGoodQuantityVO> listData = entity.getOutFromLatticeList();
                int quantitys = 0;
                for (ShelfIdAndGoodQuantityVO shelfIdAndGoodQuantityVO1 : listData) {
                    quantitys = quantitys + shelfIdAndGoodQuantityVO1.getQuantity();
                }

                ship.setShipmentQuantity(quantitys);
                ship.setMaterialFinancial(saleOrder.getMaterialFinancial());
                ship.setClientPurchasePerson(saleOrder.getClientPurchasePerson());
                ship.setClientPrNum(entity.getPrNumber());
                ship.setClientUser(saleOrder.getClientUser());
                ship.setClientMaterialNum(saleOrder.getProductNum());
                ship.setClientPicNum(saleOrder.getClientPicNum());
                ship.setClientFactoryCode(saleOrder.getClientFactoryCode());
                ship.setClientOrderRow(saleOrder.getOrderNum() + saleOrder.getOrderRow());
                ship.setWbsNum(entity.getWbsNumber());
                ship.setSaleOrder(saleOrder.getOrderNum());
                Long currentUserId = IamSecurityUtils.getCurrentUserId();
                IamUser iamUser = iamUserMapper.selectById(currentUserId);
                ship.setCreateBy(iamUser.getRealname());
                shipMapper.insert(ship);
            } else if (entity.getOutboundType().equals("ZSCK")) {

                QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
                goodsQueryWrapper.eq("goods_code", shelfIdAndGoodQuantityVO.getGoodsCode());
                Goods goods = goodsMapper.selectOne(goodsQueryWrapper);

                MiscShipment miscShipment = new MiscShipment();
                miscShipment.setReceiptTypeName(entity.getReceiptTypeName());
                miscShipment.setProductNum(goods.getGoodsCode());
                miscShipment.setProductName(goods.getGoodsName());
                miscShipment.setProductSpecification(goods.getSpecifications());

                List<ShelfIdAndGoodQuantityVO> listData = entity.getOutFromLatticeList();
                int quantitys = 0;
                for (ShelfIdAndGoodQuantityVO shelfIdAndGoodQuantityVO1 : listData) {
                    quantitys = quantitys + shelfIdAndGoodQuantityVO1.getQuantity();
                }

                LocalDate localDate = LocalDate.parse(currentDate.format(formatter), formatter);
                Date date = java.sql.Date.valueOf(localDate);
                miscShipment.setReceiveQuantity(quantitys);
                miscShipment.setReceiptNum("Mis" + currentDate.format(formatter).replaceAll("-", ""));
                miscShipment.setBusinessDate(date);
                miscShipment.setBenefitDepartmentNum(entity.getBenefitDepartmentNums());
                miscShipment.setBenefitDepartmentName(entity.getBenefitDepartmentName());
                miscShipment.setIsProduct(entity.getIsProduct());
                miscShipment.setProductType(entity.getProductType());
                miscShipment.setLocationNum(entity.getLocationNum());
                miscShipment.setLocationName(entity.getLocationName());
                miscShipmentMapper.insert(miscShipment);

            }
            outbound.setLabel(lattice.getLabel());
        }

        List<ShelfIdAndGoodQuantityVO> outFromLatticeList = entity.getOutFromLatticeList();
        //默认拿第一个物料
        ShelfIdAndGoodQuantityVO firstGoods = outFromLatticeList.get(0);

        //签收表
        QueryWrapper<Outbound> outboundQueryWrapper = new QueryWrapper<>();
        outboundQueryWrapper.eq("outbound_num", entity.getOutboundNum());
        Outbound outboundInfo = outboundMapper.selectOne(outboundQueryWrapper);
        Receive receive = new Receive();
        receive.setOutboundNum(entity.getOutboundNum());
        receive.setTaskNum(entity.getTaskNum());
        receive.setPlanNum(entity.getPlanNum());
        receive.setGoodsCode(firstGoods.getGoodsCode());
        receive.setQuantity(quantity);
        receive.setOutboundId(outboundInfo.getId());
        receive.setIsRecive(false);
        receive.setGoodsName(firstGoods.getGoodsName());
        receive.setSpecifications(firstGoods.getSpecifications());
        receive.setOutboundType(entity.getOutboundType());
        receiveMapper.insert(receive);


        outbound.setReceiptNum(entity.getReceiptNum());
        outbound.setQuantity(quantity);
        outbound.setId(entity.getId());
        outboundMapper.updateById(outbound);
        return JsonResult.OK();
    }

    @Override
    public JsonResult unOutbound(Outbound entity) {
        if ("CXCK".equals(outboundMapper.selectById(entity.getId()).getCancelType())) {
            return JsonResult.OK("该出库单已撤销，请勿重复提交撤销申请");
        }
        //修改出库单表类型为撤销出库
        outboundMapper.updateById(entity);
        //出库货格表记录删除
        outboundLatticeMapper.deleteByOutBoundId(entity.getId());
        QueryWrapper<OutboundRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("outbound_id", entity.getId());
        List<OutboundRecord> outboundRecords = outboundRecordMapper.selectList(wrapper);
        for (OutboundRecord outboundRecord : outboundRecords) {
            //商品库存表更新
            QueryWrapper<LatticeGoods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("warehouse_lattice_id", outboundRecord.getWarehouseLatticeId());
            queryWrapper.eq("goods_id", outboundRecord.getGoodsId());
            LatticeGoods latticeGoods = latticeGoodsMapper.selectOne(queryWrapper);
            if (latticeGoods == null) {
                LatticeGoods latticeGoods1 = new LatticeGoods();
                latticeGoods1.setGoodsId(outboundRecord.getGoodsId());
                latticeGoods1.setWarehouseLatticeId(outboundRecord.getWarehouseLatticeId());
                latticeGoods1.setQuantity(outboundRecord.getQuantity());
                latticeGoods1.setLable(latticeMapper.selectById(outboundRecord.getWarehouseLatticeId()).getLabel());
                latticeGoodsMapper.insert(latticeGoods1);
            } else {
                latticeGoods.setQuantity(latticeGoods.getQuantity() + outboundRecord.getQuantity());
                latticeGoodsMapper.updateById(latticeGoods);
            }


            //操作记录表新增撤销出库记录
            outboundRecord.setId(null);
            outboundRecord.setQuantity(0 - outboundRecord.getQuantity());
            outboundRecord.setType(entity.getCancelType());
            outboundRecordMapper.insert(outboundRecord);

            //计划用料表已领料数量更新
            /*QueryWrapper<PlanDemandQuantity> planDemandQuantityQueryWrapper = new QueryWrapper<>();
            planDemandQuantityQueryWrapper.eq("worksheet_id", entity.getWorksheetId());
            planDemandQuantityQueryWrapper.eq("goods_id", outboundRecord.getGoodsId());
            PlanDemandQuantity planDemandQuantity = planDemandQuantityMapper.selectOne(planDemandQuantityQueryWrapper);
            planDemandQuantity.setRecipientsQuantity(planDemandQuantity.getRecipientsQuantity() + outboundRecord.getQuantity());
            planDemandQuantityMapper.updateById(planDemandQuantity);*/
        }
        return JsonResult.OK("撤销成功");
    }

    @Override
    public JsonResult getDetail(Long id) {
        PicklistDetailVO picklistDetailVO = Binder.convertAndBindRelations(picklistMapper.selectById(id), PicklistDetailVO.class);
        //出库单号
        picklistDetailVO.setOutboundNum("CKD" + System.currentTimeMillis());

        //计划用料
        QueryWrapper<PlanDemandQuantity> wrapper = new QueryWrapper<>();
        wrapper.eq("worksheet_id", picklistDetailVO.getWorksheetId());
        List<PlanDemandQuantityDetailVO> planDemandQuantityDetailVOS = Binder.convertAndBindRelations(planDemandQuantityMapper.selectList(wrapper), PlanDemandQuantityDetailVO.class);

        for (PlanDemandQuantityDetailVO planDemandQuantityDetailVO : planDemandQuantityDetailVOS) {
            QueryWrapper<LatticeGoods> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("goods_id", planDemandQuantityDetailVO.getGoodsId());
            queryWrapper.ne("quantity", 0);
            List<LatticeGoods> latticeGoods = latticeGoodsMapper.selectList(queryWrapper);
            planDemandQuantityDetailVO.setLatticeGoods(latticeGoods);
        }
        picklistDetailVO.setPlanDemandQuantityDetailVOS(planDemandQuantityDetailVOS);

        return JsonResult.OK(picklistDetailVO);
    }

    @Override
    public JsonResult receive(Long outboundId) {
        Outbound outbound = outboundMapper.selectById(outboundId);
        outbound.setReciveUserId(IamSecurityUtils.getCurrentUserId());
        outboundMapper.updateById(outbound);

        LambdaUpdateWrapper<OutboundRecord> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(OutboundRecord::getOutboundId, outbound.getId()).set(OutboundRecord::getReciveUserId, IamSecurityUtils.getCurrentUserId());
        outboundRecordMapper.update(null, lambdaUpdateWrapper);
        return JsonResult.OK("签收成功");
    }

    @Override
    public JsonResult getOutboundDetail(Long id) {
        QueryWrapper<Outbound> wrapper = new QueryWrapper<>();
        wrapper.eq("picklist_id", id);
        wrapper.eq("is_deleted", 0);
        return JsonResult.OK(Binder.convertAndBindRelations(outboundMapper.selectList(wrapper), OutboundListVO.class));
    }

    @Override
    public JsonResult getProductInfoByTaskNum(String taskNum) {
        HashMap<String, Object> hashMap = new HashMap<>();
        QueryWrapper<ProductTask> productTaskQueryWrapper = new QueryWrapper<>();
        productTaskQueryWrapper.eq("task_num", taskNum);
        ProductTask productTask = productTaskMapper.selectOne(productTaskQueryWrapper);
        QueryWrapper<LatticeGoods> latticeGoodsQueryWrapper = new QueryWrapper<>();
        latticeGoodsQueryWrapper.eq("task_num", taskNum);
        List<LatticeGoods> goodsList = latticeGoodsMapper.selectList(latticeGoodsQueryWrapper);
        hashMap.put("productTask", productTask);
        hashMap.put("latticeGoodsList", goodsList);
        return JsonResult.OK(hashMap);
    }

    @Override
    public JsonResult getOutboundInfo(String outboundNum) {
        QueryWrapper<Outbound> wrapper = new QueryWrapper<>();
        wrapper.eq("outbound_num", outboundNum);
        Outbound outbound = outboundMapper.selectOne(wrapper);
        OutboundDetailVO outboundDetailVO = new OutboundDetailVO();
        if (outbound != null) {
            BeanUtils.copyProperties(outbound, outboundDetailVO);
            List<ShelfIdAndGoodQuantityVO> shelfIdAndGoodQuantityVOS = new ArrayList<>();
            QueryWrapper<OutboundLattice> latticeQueryWrapper = new QueryWrapper<>();
            latticeQueryWrapper.eq("outbound_id", outbound.getId());
            List<OutboundLattice> outboundLattices = outboundLatticeMapper.selectList(latticeQueryWrapper);
            for (OutboundLattice outboundLattice : outboundLattices) {
                ShelfIdAndGoodQuantityVO shelfIdAndGoodQuantityVO = new ShelfIdAndGoodQuantityVO();
                shelfIdAndGoodQuantityVO.setLatticeId(outboundLattice.getWarehouseLatticeId());
                shelfIdAndGoodQuantityVO.setQuantity(outboundLattice.getQuantity());
                shelfIdAndGoodQuantityVO.setLabel(outboundLattice.getLabel());
                Goods goods = goodsMapper.selectById(outboundLattice.getGoodsId());
                shelfIdAndGoodQuantityVO.setGoodsCode(goods.getGoodsCode());
                shelfIdAndGoodQuantityVO.setGoodsName(goods.getGoodsName());
                shelfIdAndGoodQuantityVO.setGoodsId(goods.getId());
                shelfIdAndGoodQuantityVO.setSpecifications(goods.getSpecifications());
                shelfIdAndGoodQuantityVOS.add(shelfIdAndGoodQuantityVO);
            }
            outboundDetailVO.setShelfIdAndGoodsQuantityVOList(shelfIdAndGoodQuantityVOS);
        }
        return JsonResult.OK(outboundDetailVO);
    }

    @Override
    public JsonResult getProductInfoByReceiptNum(String receiptNum) {
//        ArrayList<LatticeGoods> latticeGoodsList = new ArrayList<>();
//        QueryWrapper<ProductTask> productTaskQueryWrapper = new QueryWrapper<>();
//        productTaskQueryWrapper.eq("receipt_num", receiptNum);
//        List<ProductTask> productTaskList = productTaskMapper.selectList(productTaskQueryWrapper);
//        for (ProductTask productTask : productTaskList) {
//            String stockName = productTask.getStockName();
//            QueryWrapper<LatticeGoods> latticeGoodsQueryWrapper = new QueryWrapper<>();
//            latticeGoodsQueryWrapper.eq("stockName", stockName);
//            LatticeGoods latticeGood = latticeGoodsMapper.selectOne(latticeGoodsQueryWrapper);
//            if (latticeGood == null) {
//                return JsonResult.FAIL_EXCEPTION("商品无库存");
//            } else {
//                latticeGoodsList.add(latticeGood);
//            }
//        }
        OutboundDetailVO outboundDetailVO = new OutboundDetailVO();
        ArrayList<Object> list = new ArrayList<>();
        QueryWrapper<ProductTask> productTaskQueryWrapper = new QueryWrapper<>();
        productTaskQueryWrapper.eq("receipt_num", receiptNum);
        List<ProductTask> productTaskList = productTaskMapper.selectList(productTaskQueryWrapper);
        for (ProductTask productTask : productTaskList) {
            //拿到每一个生产任务，查询该生产任务的备料料号，去库存查询
            QueryWrapper<LatticeGoods> latticeGoodsQueryWrapper = new QueryWrapper<>();
            //生产任务中的备料料号就是库存中物料的物料编码
            latticeGoodsQueryWrapper.eq("goods_code", productTask.getStockNum());
            List<LatticeGoods> latticeGoodsList = latticeGoodsMapper.selectList(latticeGoodsQueryWrapper);
            //查到有库存的时候才去添加list
            if (latticeGoodsList.size() > 0) {
                list.add(latticeGoodsList);
            }
        }
//        List<LatticeGoods> latticeGoods = latticeGoodsMapper.selectByReceiptNum(receiptNum);
        return JsonResult.OK(list);
    }

    //  根据任务单号查询库存信息，
    @Override
    public JsonResult getProductShipInfoByOrderNum(String taskNum) {
        QueryWrapper<SaleOrder> saleOrderQueryWrapper = new QueryWrapper<>();
        saleOrderQueryWrapper.eq("order_num", taskNum.split("&")[0]);
        saleOrderQueryWrapper.eq("order_row", taskNum.split("&")[1]);
        SaleOrder saleOrder = saleOrderMapper.selectOne(saleOrderQueryWrapper);
        List<LatticeGoods> latticeGoods = new ArrayList<>();
        if (saleOrder != null) {
            String goodsCode = saleOrder.getProductNum();
            QueryWrapper<LatticeGoods> latticeGoodsQueryWrapper = new QueryWrapper<>();
            latticeGoodsQueryWrapper.eq("goods_code", goodsCode);
            latticeGoods = latticeGoodsMapper.selectList(latticeGoodsQueryWrapper);
        }
        return JsonResult.OK(latticeGoods);
    }


    @Override
    public JsonResult getMiscShipmentInfos(String goodsCode) {
        List<LatticeGoods> latticeGoods = new ArrayList<>();
        QueryWrapper<LatticeGoods> latticeGoodsQueryWrapper = new QueryWrapper<>();
        latticeGoodsQueryWrapper.eq("goods_code", goodsCode);
        latticeGoods = latticeGoodsMapper.selectList(latticeGoodsQueryWrapper);
        return JsonResult.OK(latticeGoods);
    }


    //根据销售订单号查询出库数量之和
    @Override
    public JsonResult getTotalQuantity(String receiptNum) {
        String[] split = receiptNum.split("&");
        Integer total = 0;
        QueryWrapper<Outbound> outboundQueryWrapper = new QueryWrapper<>();
        outboundQueryWrapper.eq("receipt_num", split[0]);
        outboundQueryWrapper.eq("goods_code", split[1]);
        List<Outbound> outboundList = outboundMapper.selectList(outboundQueryWrapper);
        for (Outbound outbound : outboundList) {
            total += outbound.getQuantity();
        }
        return JsonResult.OK(total);
    }

    //根据生产单据编号查询出库数量之和
    @Override
    public JsonResult getTotalQuantityBySO(String taskNumAndGoodsCode) {
        String[] split = taskNumAndGoodsCode.split("&");
        Integer total = 0;
        QueryWrapper<OutboundRecord> outboundRecordQueryWrapper = new QueryWrapper<>();
        outboundRecordQueryWrapper.eq("task_num", split[0]);
        outboundRecordQueryWrapper.eq("goods_code", split[1]);
        List<OutboundRecord> outboundRecordList = outboundRecordMapper.selectList(outboundRecordQueryWrapper);
        for (OutboundRecord outboundRecord : outboundRecordList) {
            total += outboundRecord.getQuantity();
        }
        return JsonResult.OK(total);
    }

    /*
    销售出库、杂收出库
     */
    @Override
    public JsonResult outbound(List<Outbound> outboundList) {
        //新建出库
//        super.createEntity(outbound);
        for (Outbound outbound : outboundList) {
            QueryWrapper<Lattice> latticeQueryWrapper = new QueryWrapper<>();
            latticeQueryWrapper.eq("label", outbound.getLabel());
            Lattice lattice = latticeMapper.selectOne(latticeQueryWrapper);
            if (lattice == null) {
                return JsonResult.FAIL_NOT_FOUND("二维码对应的库位存在异常");
            }
        }
        return null;
    }

    @Override
    public JsonResult getSaleOrderInfo(String orderNumAndRow) {
        String[] split = orderNumAndRow.split("&");
        QueryWrapper<SaleOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num", split[0]);
        wrapper.eq("order_row", split[1]);
        SaleOrder saleOrder = saleOrderMapper.selectOne(wrapper);
        return JsonResult.OK(saleOrder);
    }

}
